package com.hl.sfzy.controller.content;


import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.content.PublicWelfare;
import com.hl.sfzy.service.content.PublicWelfareService;
import com.hl.sfzy.utils.FileUpUtils;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Optional;

@RestController
@RequestMapping("/publicWelfare")
public class PublicWelfareController {


    @Autowired
    private PublicWelfareService publicWelfareService;


    @Value("${article.banner.img.dir.path}")
    private String banner_url;


    @GetMapping("/getAll")
    public R<PageInfo<PublicWelfare>> getAll(Integer pageNum, Integer pageSize) {
        PageInfo<PublicWelfare> all = publicWelfareService.getAll(pageNum, pageSize);
        return all.getList().size() > 0 ? R.success(all) : R.failure("查询失败");
    }


    @GetMapping("/getById/{id}")
    public R<PublicWelfare> getById(@PathVariable("id") Integer id) {
        System.out.println(id);
        PublicWelfare byId = publicWelfareService.getById(id);
        return byId != null ? R.success(byId) : R.failure("查询失败");
    }


    @PostMapping("/add")
    public R<String> addpublicWelfare(@RequestBody PublicWelfare publicWelfare) {
        System.out.println(publicWelfare);
        int add = publicWelfareService.add(publicWelfare);
        return add > 0 ? R.success("添加成功") : R.failure("添加失败");
    }


    @GetMapping("down")
    public void down(HttpServletResponse response, String fileName) throws IOException {
        // 原生servlet
        File file = new File(fileName);
        InputStream inputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        FileCopyUtils.copy(inputStream, outputStream);
    }

    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable("id") Integer id) {
        int delete = publicWelfareService.delete(id);
        return delete > 0 ? R.success("删除成功") : R.failure("删除失败");
    }


    @PostMapping("/fileup")
    public R<String> articleImgUpload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()){
            return R.failure("文件为空");
        }
        String fileName = FileUpUtils.fileUp(file, banner_url);
        return R.success(banner_url +"/"+fileName);
    }


    @GetMapping("/searchpublicWelfare")
    public R<PageInfo<PublicWelfare>> searchArticles(@RequestParam("q") String key,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime starTime,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                                               @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        PageInfo<PublicWelfare> pageInfo = publicWelfareService.search(key, starTime, endTime, pageNum, pageSize);
        return Optional.ofNullable(pageInfo).map(R::success).orElseGet(() -> R.failure("查询失败"));
    }


    @PutMapping("/update")
    public R<String> update(@RequestBody PublicWelfare publicWelfare) {
        int update = publicWelfareService.update(publicWelfare);
        return update > 0 ? R.success("更新成功") : R.failure("更新失败");
    }

}
